package cn.schoolwow.quickdao.module.operation.flow.generateEntity.single;

import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.operation.GenerateJavaSourceFileRequest;
import cn.schoolwow.quickdao.util.StringUtil;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class SetEntityClassNameFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        GenerateJavaSourceFileRequest generateJavaSourceFileRequest = (GenerateJavaSourceFileRequest) flowContext.checkData("generateJavaSourceFileRequest");
        Entity databaseEntity = (Entity) flowContext.checkData("databaseEntity");

        String entityClassName = StringUtil.underline2Camel(databaseEntity.tableName);
        entityClassName = entityClassName.toUpperCase().charAt(0)+entityClassName.substring(1);
        if(null!=generateJavaSourceFileRequest.entityClassNameMapping){
            String newEntityClassName = generateJavaSourceFileRequest.entityClassNameMapping.apply(databaseEntity,entityClassName);
            if(null!=newEntityClassName&&!newEntityClassName.isEmpty()){
                entityClassName = newEntityClassName;
            }
        }
        flowContext.putTemporaryData("entityClassName", entityClassName);
    }

    @Override
    public String name() {
        return "设置实体类名";
    }
}
